const db=wx.cloud.database().collection('goodsDetail')
const _=wx.cloud.database().command

Page({
  data:{
    tabs: [
      {
        id: 0,
        value: "综合",
        isActive: true
      }, {
        id: 1,
        value: "销量",
        isActive: false
      }, {
        id: 2,
        value: "价格",
        isActive: false
      }
    ],
    goodsList: []
  },
 onLoad:function(e) {
    console.log(e);
    this.getGoodsListByAll(e)
    // this.setData({e})
    this.getGoodsListByPrice(e)
    this.getGoodsListBySale(e)
  },
    // 切换tab栏
    tabsItemChange(e) {
      const { index } = e.detail
      let { tabs,goodsList, GoodsListBySale, GoodsListByPrice,GoodsListByAll} = this.data
      tabs.forEach((v, i) => { 
       if( i === index ) {
        v.isActive = true
       } else {
        v.isActive = false
       }
       if (index===0) {
         goodsList = GoodsListByAll
       } else if (index===1) {
        goodsList=GoodsListBySale
       } else {
        goodsList= GoodsListByPrice
       }
      })
      this.setData({
        tabs: tabs,
        goodsList
      })
    },
    //根据价格获取
    getGoodsListByPrice(e) {
      db.where(_.or([
        {
        category_id:parseInt(e.category_id)
      }, {
        classify_id:parseInt(e.classify_id)
      }
     ])).orderBy('goods_price','acs')
      .get()
      .then(res=>{
        console.log(res);
        this.setData({
          GoodsListByPrice:res.data
        })
      })
    },
    
    //根据销量获取
  getGoodsListBySale(e) {
    db.where(_.or([
      {
      category_id:parseInt(e.category_id)
    }, {
      classify_id:parseInt(e.classify_id)
    }
   ])).orderBy('saleNum','desc')
    .get()
    .then(res=>{
      console.log(res);
      this.setData({
        GoodsListBySale:res.data
      })
    })
  },
  //根据综合获取
  getGoodsListByAll(e) {
    db.where(_.or([
      {
      category_id:parseInt(e.category_id)
    }, {
      classify_id:parseInt(e.classify_id)
    }
   ]))
    .get()
    .then(res=>{
      console.log(res);
      this.setData({
        GoodsListByAll:res.data,
        goodsList:res.data
      })
    })
  }
})
 